package cn.shenlong.portal.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import cn.shenlong.common.Constants;
import cn.shenlong.common.Page;
import cn.shenlong.common.Pagination;
import cn.shenlong.portal.domain.Icon;
import cn.shenlong.portal.service.IconService;
import cn.shenlong.util.UploadUtil;

@Controller
@RequestMapping("/icon/admin")
public class IconAController {
	@Autowired
	private IconService iconService;
	
	@RequestMapping("/index")
	public String index(){
		return "/server/icon/index";
	}
	
	@RequestMapping("/list")
	public String main(Icon icon,String searchProperty, String searchValue,Integer pageSize, Integer pageNumber,ModelMap model){
		if(pageSize==null){
			pageSize = 20;
		}
		if(pageNumber==null){
			pageNumber = 1;
		}
		
//		Pagination p  = iconService.findIcon(icon, null, pageSize, pageNumber);
		
		Map<String,Object> parm = new HashMap<String,Object>();
		if(searchProperty!=null){
			parm.put(searchProperty, searchValue);
			model.put("searchValue", searchValue);
			parm.put("searchProperty", searchProperty);
			model.put("searchProperty", searchProperty);
		}
		Page p = iconService.findPageByParm(parm, pageNumber, pageSize);
		model.put("page", p);
		return "/server/icon/list";
	}
	
	@RequestMapping("/addIcon")
	public String addIcon(){
		return "/server/icon/add";
	}
	
	@RequestMapping("/showIcon")
	public String showIcon(Long iconId,ModelMap model){		
		Icon icon = this.iconService.findIconByIconId(iconId);
		model.put("icon", icon);
		return "/server/icon/show";
	}
	
	@RequestMapping("/saveIcon")
	public String saveIcon(Icon icon,ModelMap model,MultipartHttpServletRequest request){
		if(icon == null || icon.getIconId() == null){ //新增
			try {
				List<Map<String, Object>> resultList = UploadUtil.uploadImage(request, Constants.ICON_IMG_URI);
				if(!resultList.isEmpty()){
					icon.setImgUrl(resultList.get(0).get("imagePath").toString());					
				}
				iconService.save(icon);
				if(icon != null) model.put("msg", "success");
				return "redirect:list.html";
			} catch (Exception e) {
				model.put("msg", "failed");
			}
			return addIcon();
		} else {//编辑
			Icon origionIcon = iconService.findIconByIconId(icon.getIconId());
			try {
				List<Map<String, Object>> resultList = UploadUtil.uploadImage(request, Constants.ICON_IMG_URI);
				if(!resultList.isEmpty()){//有上传图片
					//删除原来的图片
					UploadUtil.deleteForUrl(origionIcon.getImgUrl());
					
					//设置新的路径
					origionIcon.setImgUrl(resultList.get(0).get("imagePath").toString());
				}
				icon = iconService.updateIcon(icon);
				if(icon != null) model.put("msg", "success");
				return "redirect:list.html";
			} catch (Exception e) {
				model.put("msg", "failed");
				model.put("icon", icon);
			}
			return this.editIcon(icon.getIconId(), model);
		}
		
	}
	
	@RequestMapping("/editIcon")
	public String editIcon(Long iconId,ModelMap model){
		if(model == null || model.get("icon") == null){			
			Icon icon = this.iconService.findIconByIconId(iconId);
			model.put("icon", icon);
		}
		return "/server/icon/edit";
	}
	
	@RequestMapping("/deleteIcon")
    @ResponseBody
	public Map<String ,Object> deleteIcon(Long iconId,String iconIdStr,HttpServletRequest request, HttpServletResponse response,ModelMap model){
		Map<String,Object> resultMap = new HashMap<String,Object>();
		try{
			if (iconId == null) {				
				String[] tmp = iconIdStr.split(",");
				Long[] iconIds = new Long[tmp.length];
				for (int i = 0; i < tmp.length; i++) {
					iconIds[i] = Long.parseLong(tmp[i]);
				}
				iconService.deleteIcon(iconIds);
			}else{
				iconService.deleteIcon(new Long[]{iconId});
			}
			resultMap.put("result", "删除成功！");	
		}catch (Exception e) {
			e.printStackTrace();
			resultMap.put("result","删除失败！原因："+e.toString());
		}
		return resultMap;
	}
	
	/**
	 * ajax上传图片
	 */
	@RequestMapping("/uploadImage")
    @ResponseBody
	public Map<String,Object> uploadImage(HttpServletRequest request, HttpServletResponse response, ModelMap model){
		Map<String,Object>  resultMap = new HashMap<String,Object>();
		try{
			List<Map<String, Object>> resultList = UploadUtil.uploadImage(request,Constants.ICON_IMG_URI);	
			resultMap.put("path", resultList.get(0).get("imagePath"));
			resultMap.put("result", true);
		}catch (Exception e) {
			e.printStackTrace();
			resultMap.put("result", false);
		}		
		return resultMap;
	}
}
